'use strict';
const {getDefaultModelConfig} = require("../../utils/common");
const { parseNum} = require("../../utils");

module.exports = app => {
    const { STRING, INTEGER, BIGINT, DECIMAL } = app.Sequelize;
    const User = app.model.define(
        'User',
        {
            id: { type: BIGINT, primaryKey: true, autoIncrement: true },
            openid: STRING(255),
            phone: STRING(255),
            password: STRING(255),
            nickname: STRING(255),
            avatar_url: STRING(255),
            status: {type: INTEGER, defaultValue: 1},
            user_type: {type: INTEGER, defaultValue: 1}, // 1: 普通用户, 3: 分销商
            invite_qr_code: STRING(500),
            parent_user_id: INTEGER,
            balance: {
                type: DECIMAL, 
                defaultValue: 0,
                get() {
                    return parseNum(this.getDataValue('balance'));
                }
            },
            ...getDefaultModelConfig(app)
        },
        {
            tableName: 'user',
            timestamps: true,
            createdAt: 'create_time',
            updatedAt: 'update_time'
        }
    );
    User.associate = function () {
        User.belongsTo(app.model.Business.User, { foreignKey: 'parent_user_id', targetKey: 'id', as: 'parent' });
    };
    return User;
};
